What is env-paths?
The env-paths npm package is used to generate paths for storing things like data, config, cache, etc., in a way that complies with the operating system's conventions. It is useful for developers who want to ensure their applications follow the best practices for file storage locations on different platforms.
What are env-paths's main functionalities?
Config Paths
Generates paths for storing configuration files. The 'config' property will provide a path that is suitable for the user's operating system.
{"config": require('env-paths')('MyApp').config}
Data Paths
Generates paths for storing data files. The 'data' property will give a path that is appropriate for the user's operating system.
{"data": require('env-paths')('MyApp').data}
Cache Paths
Generates paths for storing cache files. The 'cache' property will provide a path that is suitable for the user's operating system.
{"cache": require('env-paths')('MyApp').cache}
Log Paths
Generates paths for storing log files. The 'log' property will give a path that is appropriate for the user's operating system.
{"log": require('env-paths')('MyApp').log}
Temporary Paths
Generates paths for storing temporary files. The 'temp' property will provide a path that is suitable for the user's operating system.
{"temp": require('env-paths')('MyApp').temp}
Other packages similar to env-paths
appdata-path
Similar to env-paths, appdata-path is used to find the path to the user's app data directory. It is more focused on this single use case and does not provide the variety of paths that env-paths does.
osenv
osenv is another package that provides a way to get environment settings like paths for temporary files and user's home directory. It is similar to env-paths but has a broader scope, including network settings and shell paths.
userhome
userhome is a simple package to get the path to the user's home directory. It is much more limited in scope compared to env-paths, which provides paths for various types of application files.
env-paths
Get paths for storing things like data, config, cache, etc
Uses the correct OS-specific paths. Most developers get this wrong.
Install
$ npm install env-paths
Usage
const envPaths = require('env-paths');
const paths = envPaths('MyApp');
paths.data;
paths.config
API
paths = envPaths(name, options?)
Note: It only generates the path strings. It doesn't create the directories for you. You could use make-dir
to create the directories.
name
Type: string
Name of your project. Used to generate the paths.
options
Type: object
suffix
Type: string
Default: 'nodejs'
Don't use this option unless you really have to!
Suffix appended to the project name to avoid name conflicts with native
apps. Pass an empty string to disable it.
paths.data
Directory for data files.
Example locations (with the default nodejs
suffix):
- macOS:
~/Library/Application Support/MyApp-nodejs
- Windows:
%LOCALAPPDATA%\MyApp-nodejs\Data
(for example, C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Data
) - Linux:
~/.local/share/MyApp-nodejs
(or $XDG_DATA_HOME/MyApp-nodejs
)
paths.config
Directory for config files.
Example locations (with the default nodejs
suffix):
- macOS:
~/Library/Preferences/MyApp-nodejs
- Windows:
%APPDATA%\MyApp-nodejs\Config
(for example, C:\Users\USERNAME\AppData\Roaming\MyApp-nodejs\Config
) - Linux:
~/.config/MyApp-nodejs
(or $XDG_CONFIG_HOME/MyApp-nodejs
)
paths.cache
Directory for non-essential data files.
Example locations (with the default nodejs
suffix):
- macOS:
~/Library/Caches/MyApp-nodejs
- Windows:
%LOCALAPPDATA%\MyApp-nodejs\Cache
(for example, C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Cache
) - Linux:
~/.cache/MyApp-nodejs
(or $XDG_CACHE_HOME/MyApp-nodejs
)
paths.log
Directory for log files.
Example locations (with the default nodejs
suffix):
- macOS:
~/Library/Logs/MyApp-nodejs
- Windows:
%LOCALAPPDATA%\MyApp-nodejs\Log
(for example, C:\Users\USERNAME\AppData\Local\MyApp-nodejs\Log
) - Linux:
~/.local/state/MyApp-nodejs
(or $XDG_STATE_HOME/MyApp-nodejs
)
paths.temp
Directory for temporary files.
Example locations (with the default nodejs
suffix):
- macOS:
/var/folders/jf/f2twvvvs5jl_m49tf034ffpw0000gn/T/MyApp-nodejs
- Windows:
%LOCALAPPDATA%\Temp\MyApp-nodejs
(for example, C:\Users\USERNAME\AppData\Local\Temp\MyApp-nodejs
) - Linux:
/tmp/USERNAME/MyApp-nodejs